package com.yanqu.road.server.manger.maidian.biaoju;

import com.yanqu.road.entity.activity.biaoju.config.BiaoJuDartConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;
import com.yanqu.road.pb.activity.BiaoJuProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.biaoju.BiaoJuConfigMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.HashMap;
import java.util.Map;

public class BiaoJuLog {
    public static void traceBuildCity(GamePlayer player, int activityId, BiaoJuProto.BiaoJuCityBuildRespMsg respMsg) {
        try {
            DefaultEventTraceAction action = new DefaultEventTraceAction("escort_build", player);
            action.build("activityid", activityId);
            action.build("escort_id", respMsg.getUnionInfo().getUnionId());
            action.build("escort_buildid", respMsg.getCityId());
            action.build("escort_buildconsume", respMsg.getNum());
            action.build("escort_buildget", respMsg.getReward());
            action.build("escort_buildinf", respMsg.getNum());
            action.build("escort_buildrep", respMsg.getNum());
            action.trace();

            DefaultEventTraceAction action2 = new DefaultEventTraceAction("escort_reputation", player);
            action2.build("activityid", activityId);
            action2.build("escort_id", respMsg.getUnionInfo().getUnionId());
            action2.build("escort_repuid", player.getUserId());
            action2.build("escort_repgive", respMsg.getNum());
            action2.build("escort_repnow", respMsg.getUnionInfo().getUnionExp());
            action2.build("escort_replevel", respMsg.getUnionInfo().getUnionLevel());
            action2.trace();
        } catch (Exception e) {
            BiaoJuConfigMgr.getLogger().error("trace err {}", e);
        }
    }

    public static void traceRefresh(GamePlayer player, int activityId, BiaoJuProto.CrossBiaoJuRefreshEscortRespMsg respMsg) {
        try {
            BiaoJuConfig config = BiaoJuConfigMgr.getConfig();
            if (config == null) {
                return;
            }
            Map<Integer, Integer> qualityMap = new HashMap<>();
            for (BiaoJuProto.BiaoJuWaitTakeEscortTemp waitTakeEscortTemp : respMsg.getRespMsg().getWaitTakeListList()) {
                BiaoJuDartConfig dartConfig = config.getDartConfig(waitTakeEscortTemp.getEscortId());
                if (dartConfig == null) {
                    continue;
                }
                qualityMap.put(dartConfig.getQuality(), dartConfig.getId());
            }

            DefaultEventTraceAction action = new DefaultEventTraceAction("escort_refresh", player);
            action.build("activityid", activityId);
            action.build("escort_reftype", respMsg.getIsCostGoods() ? 1 : 0);
            action.build("escort_refpond", qualityMap);
            action.build("escort_refisget", respMsg.getRespMsg().getIsRefreshBest());
            action.trace();
        } catch (Exception e) {
            BiaoJuConfigMgr.getLogger().error("trace err {}", e);
        }
    }

    public static void traceEscortEnd(GamePlayer player, BiaoJuProto.CrossBiaoJuRunEndSyncMsg respMsg) {
        try {
            BiaoJuConfig config = BiaoJuConfigMgr.getConfig();
            if (config == null) {
                return;
            }

            int speedupGoodsId = config.getSpeedupGoodsId();
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(speedupGoodsId);
            long perSpeedUpTime = goodsInfo.getParamList().get(0).longValue() * DateHelper.MINUTE_MILLIONS;
            long speedUpNum = respMsg.getSpeedUpTime() / perSpeedUpTime;

            BiaoJuDartConfig dartConfig = config.getDartConfig(respMsg.getEscortId());
            Map<String, Integer> qualityMap = new HashMap<>();
            qualityMap.put(String.valueOf(dartConfig.getQuality()), dartConfig.getId());

            DefaultEventTraceAction action = new DefaultEventTraceAction("escort_settle", player);
            action.build("activityid", config.getActivityId());
            action.build("escort_settleid", respMsg.getEscortUniqueId());
            action.build("escort_settleinf", qualityMap);
            action.build("escort_settleuid", player.getUserId());
            action.build("escort_settleuidmap", respMsg.getMembers());
            action.build("escort_settleeventmap", respMsg.getBattleLogs());
            action.build("escort_settleresult", respMsg.getFinishLevel());
            action.build("escort_settlespeed", speedUpNum);
            action.build("escort_settle1add", respMsg.getTitleAddition());
            action.build("escort_settle2add", respMsg.getUnionLevelAddition());
            action.build("escort_settle3add", respMsg.getCityAddition());
            action.build("escort_settle4add", respMsg.getLeaderAddition());
            action.build("escort_settlegetexp", respMsg.getExpAdd());
            action.build("escort_settleget", respMsg.getMoneyAdd());
            action.trace();

            DefaultEventTraceAction action2 = new DefaultEventTraceAction("escort_exp", player);
            action2.build("activityid", config.getActivityId());
            action2.build("escort_expadd", respMsg.getExpAdd());
            action2.build("escort_expnow", respMsg.getNowExp());
            action2.build("escort_explevel", respMsg.getNowLevel());
            action2.trace();
        } catch (Exception e) {
            BiaoJuConfigMgr.getLogger().error("trace err {}", e);
        }
    }
}
